import * as types from '../types'

export const user = {
  state: {
    // 用户登录信息
    userInfo: userInfo()
  },
  getters: {
    userInfo: state => state.userInfo
  },
  actions: {
    /**
     * 用户登录
     */
    setUserInfo ({ commit }, res) {
      let Obj = userInfo()
      Obj = Object.assign(Obj, res)

      let data = JSON.stringify(Obj)
      sessionStorage.setItem('userInfo', data)
      localStorage.setItem('userInfo', data)
      commit(types.SET_USER_INFO, Obj)
    }
  },
  mutations: {
    [types.SET_USER_INFO] (state, res) {
      state.userInfo = res
    }
  }
}
function userInfo () {
  return (localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo'))) || {}
}
